package com.netease.financial.base.push;

import android.os.Bundle;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by wyf on 8/7/15.
 */
public class PushUtils {

    public static String getPushMessage(Bundle bundle) {
        return bundle.getString(JPushInterface.EXTRA_MESSAGE);
    }

    public static JSONObject getExtraJSONObject(Bundle bundle) {
        String jsonStr = bundle.getString(JPushInterface.EXTRA_EXTRA);
        JSONObject jsonObject = new JSONObject();

        if (TextUtils.isEmpty(jsonStr)) {
            return jsonObject;
        }
        try {
            jsonObject = new JSONObject(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    public static String getPushType(JSONObject jsonObject) {
        return jsonObject.optString(PushConstants.JS_KEY_PUSH_TYPE, PushConstants.PUSH_TYPE_DEFAULT);
    }

    public static int getBadgeNumber(JSONObject jsonObject) {
        return jsonObject.optInt(PushConstants.JS_KEY_BADGE, 0);
    }

    public static boolean isValidPushType(String pushType) {
        return !TextUtils.isEmpty(pushType) && !PushConstants.PUSH_TYPE_DEFAULT.equals(pushType);
    }

}
